package com.example.securingadmin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @author caiwl
 * @date 2020/12/8 14:52
 */
@Service
public class LoginService {
    @Autowired
    private AuthenticationManager authenticationManager;

    public String login(String username, String password) {
        // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
        Authentication authenticate = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
        LoginUser loginUser = (LoginUser) authenticate.getPrincipal();
        String token = UUID.randomUUID().toString();
        SecurityUtils.tokenMap.put(token, loginUser);
        return token;
    }
}
